/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jbr.simplefoodplan.gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.RootPanel;
import com.jbr.simplefoodplan.gwt.client.ServerCom.ElementCallback;
import com.jbr.simplefoodplan.gwt.client.ServerCom.ListCallback;
import com.jbr.simplefoodplan.gwt.client.ui.PlanUi;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Main entry point.
 *
 * @author jbr
 */
public class MainEntryPoint implements EntryPoint {
    public List<Dish> dishList = new ArrayList<Dish>();
    public final static DateTimeFormat dateFormat = DateTimeFormat.getFormat("EEEE dd.MM.yyyy");
    public final static DateTimeFormat daynameFormat = DateTimeFormat.getFormat("EEE");
    public final static DateTimeFormat jsonFormat = DateTimeFormat.getFormat("yyyy-MM-dd");
    public final Map<Date, Plan> planMap = new HashMap<Date, Plan>();

    public MainEntryPoint() {
    }

    /** 
     * The entry point method, called automatically by loading a module
     * that declares an implementing class as an entry-point
     */
    @Override
    public void onModuleLoad() {
        final PlanUi planUi = new PlanUi(this);

        updateDishList(new ChangeHandler() {

            @Override
            public void onChange(ChangeEvent event) {
                //Update listbox.
                planUi.updateListBoxes();
            }
        });


        //RootPanel.get().add(table);
        RootPanel.get().add(planUi);
    }

    public void updateDishList(final ChangeHandler ch) {
        ServerCom.getDishList(new ListCallback<Dish>() {

            @Override
            public void onResponseOk(List<Dish> list) {
                dishList = list;
                System.out.println("Dishlist received!");
                if (ch != null)
                    ch.onChange(null);
            }
        });
    }

    public void getPlanAtDate(final int index, final Date date, final ElementCallback<Plan, Integer> callback) {
        final String dateAsString = dateFormat.format(date);
        /*
        Plan get = planMap.get(date);
        System.out.println("Get: " + dateAsString + " " + get);
        if (planMap.containsKey(date)) {
            System.out.println("Found in cache: " + get);
            callback.onResponseOk(get, index);
            return;
        }
         * 
         */
        //Load to cache
        ServerCom.getPlanAtDate(index, date, new ServerCom.ElementCallback<Plan, Integer>() {

            @Override
            public void onResponseOk(Plan element, Integer backref) {
                System.out.println("Plan recieved for " + dateAsString + ": " + element);
                planMap.put(date, element);
                System.out.println("Cache now contains following number of entries: " + planMap.size());
                callback.onResponseOk(element, backref);
            }
        });
    }

    public void clearPlanCache() {
        planMap.clear();
    }

    public void addPlanInCache(Plan element) {
        System.out.println("Updating cache with: " + element);
        planMap.put(element.getDate(), element);
    }


}
